<template>
  <view style="margin-top: 100rpx;">

    <view class="header">
      <image class="image" src="/static/images/Maskgroup@2x.png" mode=""></image>
      <text>欢迎来到物流平台</text>
    </view>
    <view class="gui-text-center gui-padding gui-margin-top">
      <button open-type="getPhoneNumber" @getphonenumber="onGetPhoneNumber" class="gui-bg-blue gui-color-white"
        style="border-radius: 50rpx;">
        手机号快捷登录
      </button>

      <button @click="login" class="bg-lv gui-color-white gui-margin-top" style="border-radius: 50rpx;">
        微信登录
      </button>
    </view>

  </view>
</template>

<script>
  export default {
    data() {
      return {
        session_key: '',
        userId: ''
      }
    },
    onLoad() {
      this.wchatLogin()
    },
    methods: {
      onGetPhoneNumber(e) {
        if (e.detail.errMsg == "getPhoneNumber:fail user deny") {
          //用户决绝授权  
          //拒绝授权后弹出一些提示  
        } else {
          //允许授权 ，进行手机号解密
          let self = this
          uni.gRequest.get('/app/wchat/phone', {
            sessionKey: self.session_key,
            encryptedData: e.detail.encryptedData,
            iv: e.detail.iv
          }).then(res => {
            // console.log(res, '获取手机号');
            // 调用户修改手机号接口
            uni.gRequest.get('/app/wchat/update/phone', {
              id: self.userId,
              phone: JSON.parse(res.data).phoneNumber
            }).then(rt => {
              uni.showToast({
                title: '登录成功',
                icon: "none"
              });
              uni.gRequest.post('/app/user/edit', {
                userClass: 1
              })
              setTimeout(() => {
                uni.switchTab({
                  url: '/pages/switchPages/user'
                })
              }, 400)
            })
          })

        }

      },
      login() {
        uni.showToast({
          title: '登录成功',
          icon: "none"
        });
        uni.gRequest.post('/app/user/edit', {
          userClass: 1
        })
        setTimeout(() => {
          uni.switchTab({
            url: '/pages/switchPages/user'
          })
        }, 1000)
      },
      wchatLogin() {
        //处理推广人
        var tgcode = uni.getStorageSync("tgcode");
        let self = this
        uni.login({
          provider: 'weixin',
          success(res) {
            // console.log('res', res);
            uni.gRequest.get("/app/wchat/login", {
              code: res.code,
              tgcode: tgcode
            }).then(result => {
              // uni.showToast({
              //   title: '登录成功',
              //   icon: "none"
              // });
              self.session_key = result.data.session_key

              self.userId = result.data.userId;
              // console.log(result);
              // uni.gRequest.get(`user/info/${self.userId}`).then(res => {
              //   uni.setStorageSync("userInfo", {
              //     ...result.data,
              //     ...res.data
              //   });
              // })
              uni.setStorageSync("userInfo", result.data);
            });

          }
        })
      }
    }
  }
</script>

<style lang="scss">
  .header {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    font-size: 48rpx;
    color: #333333;
    margin-bottom: 200rpx;

    .image {
      width: 162rpx;
      height: 162rpx;
      margin-bottom: 50rpx;
    }
  }
</style>
